Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure useState and useReducer initializer functions are double invoked in StrictMode #28248

Merged
merged 5 commits into from
Feb 6, 2024

Conversation

eps1lon
Copy link
Collaborator

@eps1lon eps1lon commented Feb 5, 2024

Summary

Fixes #25583 (comment)

In StrictMode we used to double invoke state initializer functions in StrictMode (I started bisecting from 17 where this was already the case). Our docs imply this:

Functions that you pass to useState, set functions, useMemo, or useReducer
-- https://react.dev/reference/react/StrictMode#fixing-bugs-found-by-double-rendering-in-development

However, in #25583 we stopped doing that but likely because we never had a test.

Now we double invoke again using the same pattern we use for double invoking updater functions.

How did you test this change?

  • added test for state initializer invocations

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Feb 5, 2024
Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol this comment is funny now

// There are plenty of tests to ensure this behavior is correct.
const shouldDoubleRenderDEV =

@react-sizebot
Copy link

react-sizebot commented Feb 5, 2024

Comparing: f5a3158...3b83783

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js +0.14% 176.57 kB 176.82 kB +0.17% 55.00 kB 55.10 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js +0.14% 178.69 kB 178.94 kB +0.10% 55.64 kB 55.69 kB
facebook-www/ReactDOM-prod.classic.js +0.14% 591.32 kB 592.15 kB +0.15% 104.34 kB 104.49 kB
facebook-www/ReactDOM-prod.modern.js +0.14% 575.08 kB 575.91 kB +0.15% 101.43 kB 101.58 kB
test_utils/ReactAllWarnings.js Deleted 67.56 kB 0.00 kB Deleted 16.59 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
facebook-react-native/react-test-renderer/cjs/ReactTestRenderer-dev.js +0.38% 936.25 kB 939.83 kB +0.38% 187.73 kB 188.45 kB
react-native/implementations/ReactFabric-dev.js +0.37% 977.95 kB 981.53 kB +0.36% 197.71 kB 198.43 kB
react-native/implementations/ReactNativeRenderer-dev.js +0.36% 994.24 kB 997.81 kB +0.36% 201.99 kB 202.71 kB
oss-stable-semver/react-art/cjs/react-art.production.min.js +0.25% 99.72 kB 99.96 kB +0.25% 30.64 kB 30.72 kB
oss-stable/react-art/cjs/react-art.production.min.js +0.25% 99.77 kB 100.02 kB +0.26% 30.67 kB 30.75 kB
oss-experimental/react-art/cjs/react-art.production.min.js +0.24% 101.72 kB 101.97 kB +0.14% 31.27 kB 31.31 kB
facebook-www/ReactART-prod.modern.js +0.24% 350.58 kB 351.41 kB +0.25% 59.38 kB 59.53 kB
facebook-www/ReactART-prod.classic.js +0.23% 361.55 kB 362.37 kB +0.25% 61.34 kB 61.49 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.production.min.js +0.21% 115.23 kB 115.47 kB +0.23% 35.67 kB 35.75 kB
oss-stable/react-reconciler/cjs/react-reconciler.production.min.js +0.21% 115.25 kB 115.50 kB +0.23% 35.69 kB 35.77 kB
oss-experimental/react-reconciler/cjs/react-reconciler.production.min.js +0.21% 117.16 kB 117.41 kB +0.36% 36.31 kB 36.44 kB
oss-stable-semver/react-art/cjs/react-art.production.js +0.20% 597.32 kB 598.54 kB +0.21% 133.58 kB 133.86 kB
oss-stable/react-art/cjs/react-art.production.js +0.20% 597.35 kB 598.56 kB +0.21% 133.60 kB 133.89 kB
oss-experimental/react-art/cjs/react-art.production.js +0.20% 604.76 kB 605.97 kB +0.21% 135.34 kB 135.62 kB
test_utils/ReactAllWarnings.js Deleted 67.56 kB 0.00 kB Deleted 16.59 kB 0.00 kB

Generated by 🚫 dangerJS against 3b83783

@eps1lon eps1lon marked this pull request as ready for review February 5, 2024 18:43
@rickhanlonii
Copy link
Member

@eps1lon should we also add the dimming like in #28249?

@eps1lon eps1lon merged commit 97fd3e7 into facebook:main Feb 6, 2024
36 checks passed
@eps1lon eps1lon deleted the fix/strictmode-initializer branch February 6, 2024 16:53
github-actions bot pushed a commit that referenced this pull request Feb 6, 2024
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants